package servlets;

import java.io.IOException;
import java.sql.SQLException;
import java.util.List;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import modele.Personne;
import modele.Session;
import dao.PersonneDAO;
import dao.SessionDAO;


public class Connecter extends HttpServlet {
	private static final long serialVersionUID = 1L;

	public Connecter() {
		super();

	}

	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// delegue vers connecter.jsp
		getServletContext().getRequestDispatcher("/WEB-INF/connecter.jsp")
		.forward(request, response);
	}

	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// Verifie la saisie et si pas ok, positionner le
		// msgEmailPassword

		String email = request.getParameter("email");
		String password = request.getParameter("password");
	
		
		if (email == null || email.matches("^ *$") || password == null
				|| password.matches("^ *$")) {
			request.setAttribute("msgLoginPassword",
					"Email et mot de passe obligatoires");

		}
		try {
			PersonneDAO dao = new PersonneDAO();

			dao = new PersonneDAO();

			Personne user = dao.lireParLoginPassword(email, password);
			
			if (user == null) {
				request.setAttribute("msgLoginPassword",
						"Email/password incorrect");
			} else {
				request.getSession(true).setAttribute("user", user);
				System.out.println(user.getEmail());
			}

		} catch (SQLException e) {
			e.printStackTrace();
			request.setAttribute("msgLoginPassword", e.getMessage());
			System.out.println("probleme de connection" + e.getMessage());
		}
		getServletContext().getRequestDispatcher("/accueil.jsp")
				.forward(request, response);
	}

}



